package com.xky.controller;

import com.xky.pojo.*;
import com.xky.service.BookService;
import com.xky.service.PublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/publish")
public class PublishController {

    @Autowired
    private PublishService publishService;

    @Autowired
    private BookService bookService;

    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Result addPublish(@RequestBody Publish publish) {
        try {
            log.info("添加出版社信息: {}", publish);

            // 1. 参数校验
            if (publish.getBookCode() == null) {
                log.error("图书编号为空");
                return Result.error("图书编号不能为空");
            }
            if (publish.getBookName() == null || publish.getBookName().trim().isEmpty()) {
                log.error("图书名称为空");
                return Result.error("图书名称不能为空");
            }

            // 2. 检查图书编号是否已存在
            Book existingBook = bookService.selectByCode(Integer.valueOf(publish.getBookCode()));
            if (existingBook != null) {
                log.error("图书编号已存在: {}", publish.getBookCode());
                return Result.error("图书编号已被使用，请使用其他编号");
            }

            // 3. 添加出版社信息
            publishService.addPublish(publish);
            log.info("出版社信息添加成功");

            return Result.success();
        } catch (Exception e) {
            log.error("添加出版社信息失败: {}", e.getMessage(), e);
            return Result.error("添加出版社信息失败: " + e.getMessage());
        }
    }

    /**
     * 删除出版社信息
     * @param publish
     * @return
     */
    @DeleteMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public Result deletePublish(Publish publish){
        log.info("删除出版社信息{}",publish);
        publishService.deletePublish(publish);
        return Result.success();
    }

    /**
     * 批量删除出版社信息
     * @param ids 要删除的出版社ID列表
     * @return 处理结果
     */
    @DeleteMapping("/deleteIds")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteSelect(@RequestParam("ids") List<Integer> ids) {
        try {
            // 1. 参数校验
            if (ids == null || ids.isEmpty()) {
                log.error("删除列表为空");
                return Result.error("请选择要删除的出版社信息");
            }

            // 3. 执行批量删除
            log.info("开始批量删除出版社信息，要删除的ID: {}", ids);
            publishService.deleteIds(ids);
            log.info("批量删除出版社信息成功，共删除{}条记录", ids.size());

            return Result.success();
        } catch (Exception e) {
            log.error("批量删除出版社信息失败: {}", e.getMessage(), e);
            return Result.error("批量删除出版社信息失败: " + e.getMessage());
        }
    }

    /**
     * 更新出版社信息
     * @param publish
     * @return
     */
    @PutMapping("/update")
    public Result updatePublishById(@RequestBody Publish publish){
        log.info("更新出版社信息{}",publish);
        publishService.updatePublishById(publish);
        return Result.success();
    }

    /**
     * 根据id查询出版社信息
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public Result selectById(Integer id){
        log.info("根据id查询出版社信息{}",id);
      Publish result =  publishService.selectById(id);
        return Result.success(result);
    }

    /**
     * 查询全部的信息
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(){
        log.info("查询全部的信息");
       List<Publish> result = publishService.selectAll();
       return Result.success(result);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param publishName
     * @return
     */
    @GetMapping("/pages")
    public Result pages(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5")Integer pageSize,@RequestParam(required = false) String publishName){
        log.info("开始页码{}，结束页码{}，模糊的出版社名{}",page,pageSize,publishName);
      PublishBean result = publishService.pages(page,pageSize,publishName);
        log.info("查询结果：总记录数：{}, 返回数据条数：{}", result.getTotal(), result.getRows() != null ? result.getRows().size() : 0);
      return Result.success(result);
    }
}
